unit SmallStruct.View.TSSDataGridView;

interface

uses
  Windows,
  Messages,
  SysUtils,
  Variants,
  Classes,
  Graphics,
  Controls,
  Forms,
  SmallStruct.View.TSSDataView,
  cxStyles,
  cxCustomData,
  cxGraphics,
  cxFilter,
  cxData,
  cxDataStorage,
  cxEdit,
  DB,
  cxDBData,
  cxGridLevel,
  cxClasses,
  cxControls,
  cxGridCustomView,
  cxGridCustomTableView,
  cxGridTableView,
  cxGridDBTableView,
  cxGrid,
  cxDBLookupComboBox,
  SmallStruct.Form.TSSFrame,
  SmallStruct.Form.TSSPanel,
  ExtCtrls,
  RzPanel,
  cxContainer,
  cxTextEdit,
  cxMaskEdit,
  cxDropDownEdit,
  cxLookupEdit,
  cxDBLookupEdit,
  RzBckgnd,
  RzButton,
  RzBmpBtn,
  cxCheckBox,
  StdCtrls,
  SmallStruct.View,
  SmallStruct.Core,
  SmallStruct.Configuration,
  RzCmboBx;

type
  TSSDataGridView = class(TSSDataView, ISSDataGridView)
    _Panel1: TRzPanel;
    _Panel: TRzPanel;
    _Background: TRzBackground;
    _Panel2: TRzPanel;
    ApplyButtonPanel: TRzPanel;
    ApplyButton: TRzBmpButton;
    ClearButtonPanel: TRzPanel;
    ClearButton: TRzBmpButton;
    GroupCheckBoxPanel: TRzPanel;
    GroupCheckBox: TcxCheckBox;
    _StyleRepository: TcxStyleRepository;
    _BackgroundStyle: TcxStyle;
    _ContentEvenStyle: TcxStyle;
    _GroupByBoxStyle: TcxStyle;
    _SelectionStyle: TcxStyle;
    _Grid: TcxGrid;
    _GridDBTableView: TcxGridDBTableView;
    _GridLevel: TcxGridLevel;
    FieldNameListBox: TcxComboBox;
    MethodListBox: TcxComboBox;
    procedure ApplyButtonClick(Sender: TObject);
    procedure ClearButtonClick(Sender: TObject);
    procedure _GridDBTableViewKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure _GridDBTableViewKeyUp(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    Bookmark: TStringList;
    procedure BeginSelection;
    procedure EndSelection;
  protected
    procedure InitLookupComboBoxColumn(AGrid: TcxGridDBTableView; AColumnIndex: Integer; const ALinkeModelName, ALinkFieldName, ALinkKeyFieldName: string);
    procedure InitGridView(AGrid: TcxGridDBTableView; const AModelName: string);
    procedure InitToolBar(AGrid: TcxGridDBTableView);
    procedure AppendAggregate(const FieldName: string; AggregateType: TSSAggregateType; const IsGroup: Boolean);
  public
    procedure InitObject; override;
  public
    function GetSelectBookmark: TStringList;
    function GetCustomeConfiguration: ISSConfiguration;
    procedure SetCustomeConfiguration(ACustomeConfiguration: ISSConfiguration);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

implementation

uses
  SmallStruct.Model,
  SmallStruct.Configuration.TSSViewExtendConfiguration;

{$R *.dfm}

{ TSSDataGridView }

procedure TSSDataGridView.AppendAggregate(const FieldName: string; AggregateType: TSSAggregateType; const IsGroup: Boolean);
var
  SummaryItem: TcxGridDBTableSummaryItem;
begin
  _GridDBTableView.OptionsView.Footer := True;
  if IsGroup then
    SummaryItem := (_GridDBTableView.DataController.Summary.DefaultGroupSummaryItems.Add) as TcxGridDBTableSummaryItem
  else
    SummaryItem := (_GridDBTableView.DataController.Summary.FooterSummaryItems.Add) as TcxGridDBTableSummaryItem;

  SummaryItem.Column := _GridDBTableView.GetColumnByFieldName(FieldName);

  case AggregateType of
    ssatCount:
      begin
        SummaryItem.Format := '''' + FieldName + ''' Count: 0.00';
        SummaryItem.Kind := skCount;
      end;
    ssatSum:
      begin
        SummaryItem.Format := '''' + FieldName + ''' Sum: 0.00';
        SummaryItem.Kind := skSum;
      end;
    ssatAverage:
      begin
        SummaryItem.Format := '''' + FieldName + ''' Average: 0.00';
        SummaryItem.Kind := skAverage;
      end;
    ssatMax:
      begin
        SummaryItem.Format := '''' + FieldName + ''' Max: 0.00';
        SummaryItem.Kind := skMax;
      end;
    ssatMin:
      begin
        SummaryItem.Format := '''' + FieldName + ''' Min: 0.00';
        SummaryItem.Kind := skMin;
      end;
  end;

  //  _GridDBTableView.DataController.Summary.FooterSummaryItems.Add;
end;

procedure TSSDataGridView.ApplyButtonClick(Sender: TObject);
var
  FieldName: string;
  Index: Integer;
  AggregateType: TSSAggregateType;
begin
  AggregateType := ssatNone;

  if MethodListBox.Text = 'COUNT' then
    AggregateType := ssatCount
  else if MethodListBox.Text = 'SUM' then
    AggregateType := ssatSum
  else if MethodListBox.Text = 'AVERAGE' then
    AggregateType := ssatAverage
  else if MethodListBox.Text = 'MAX' then
    AggregateType := ssatMax
  else if MethodListBox.Text = 'MIN' then
    AggregateType := ssatMin;

  for Index := 0 to _GridDBTableView.ColumnCount - 1 do
    if _GridDBTableView.Columns[Index].Caption = FieldNameListBox.Text then
    begin
      FieldName := _GridDBTableView.Columns[Index].DataBinding.Field.FieldName;
      Break;
    end;

  AppendAggregate(FieldName, AggregateType, GroupCheckBox.Checked);

end;

procedure TSSDataGridView.BeginSelection;
begin
  _GridDBTableView.DataController.DataModeController.GridMode := True;
  Bookmark.Clear;
end;

procedure TSSDataGridView.ClearButtonClick(Sender: TObject);
begin
  _GridDBTableView.DataController.Summary.DefaultGroupSummaryItems.Clear;
  _GridDBTableView.DataController.Summary.FooterSummaryItems.Clear;
  _GridDBTableView.OptionsView.Footer := False;
end;

constructor TSSDataGridView.Create(AOwner: TComponent);
begin
  inherited;
  Bookmark := TStringList.Create;
end;

destructor TSSDataGridView.Destroy;
begin
  Bookmark.Free;
  inherited;
end;

procedure TSSDataGridView.EndSelection;
var
  Index: Integer;
begin
  for Index := 0 to _GridDBTableView.DataController.GetSelectedCount - 1 do
    Bookmark.Add(_GridDBTableView.DataController.GetSelectedBookmark(Index));

  _GridDBTableView.DataController.DataModeController.GridMode := False;
end;

function TSSDataGridView.GetCustomeConfiguration: ISSConfiguration;
var
  Configuration: TSSViewExtendConfiguration;
  FieldConfiguration: TSSViewExtendFieldConfiguration;
  Index: Integer;
begin
  Configuration := TSSViewExtendConfiguration.Create(nil);
  Result := Configuration;
  // Ralf
  {
    for Index := 0 to _GridDBTableView.ColumnCount - 1 do
    begin
      FieldConfiguration := TSSViewExtendFieldConfiguration.Create(nil);
      Configuration.GetFieldConfiguration.AppendObject(_GridDBTableView.Columns[Index].DataBinding.FieldName, FieldConfiguration);

      FieldConfiguration.SetFieldName(_GridDBTableView.Columns[Index].DataBinding.FieldName);
    end;
   }
end;

function TSSDataGridView.GetSelectBookmark: TStringList;
begin
  Result := Bookmark;
end;

procedure TSSDataGridView.InitGridView(AGrid: TcxGridDBTableView; const AModelName: string);
var
  ViewConfiguration: ISSViewConfiguration;
  FieldConfiguration: ISSViewFieldConfiguration;
  Index: Integer;
begin
  AGrid.DataController.DataSource := QueryDataModel(AModelName).GetDataSource;
  AGrid.ClearItems;

  ViewConfiguration := GetContext.GetConfiguration as ISSViewConfiguration;

  if ViewConfiguration.IsReadOnly then
  begin
    AGrid.OptionsData.Appending := False;
    AGrid.OptionsData.Deleting := False;
    AGrid.OptionsData.Editing := False;
    AGrid.OptionsData.Inserting := False;
  end;

  if ViewConfiguration.IsMultiDataChoose then
    AGrid.OptionsSelection.MultiSelect := True;

  for Index := 0 to ViewConfiguration.GetFieldConfiguration.GetCount - 1 do
  begin
    FieldConfiguration := ViewConfiguration.GetFieldConfiguration.GetObject(Index) as ISSViewFieldConfiguration;
    if FieldConfiguration.IsVisiable then
    begin
      AGrid.CreateColumn.DataBinding.FieldName := FieldConfiguration.GetFieldName;
      AGrid.Columns[Index].FooterAlignmentHorz := taCenter;
      AGrid.Columns[Index].HeaderAlignmentHorz := taCenter;
      AGrid.Columns[Index].HeaderAlignmentVert := vaCenter;

      AGrid.Columns[Index].HeaderGlyphAlignmentHorz := taCenter;

      AGrid.Columns[Index].ApplyBestFit;

      if AGrid.Columns[Index].Width < 120 then
        AGrid.Columns[Index].Width := 120;

      if (FieldConfiguration.GetLinkFieldName <> '') and (FieldConfiguration.GetLinkModelName <> '') and (FieldConfiguration.GetLinkKeyFieldName <> '') then
        InitLookupComboBoxColumn(AGrid, Index, FieldConfiguration.GetLinkModelName, FieldConfiguration.GetLinkFieldName, FieldConfiguration.GetLinkKeyFieldName);
    end;
  end;
end;

procedure TSSDataGridView.InitLookupComboBoxColumn(AGrid: TcxGridDBTableView; AColumnIndex: Integer; const ALinkeModelName, ALinkFieldName, ALinkKeyFieldName: string);
begin
  AGrid.Columns[AColumnIndex].PropertiesClass := TcxLookupComboBoxProperties;
  with TcxLookupComboBoxProperties(AGrid.Columns[AColumnIndex].Properties) do
  begin
    ListSource := GetDataSource(ALinkeModelName);
    ListFieldNames := ALinkFieldName;
    KeyFieldNames := ALinkKeyFieldName;
  end;
end;

procedure TSSDataGridView.InitObject;
begin
  inherited;
  InitGridView(_GridDBTableView, (GetContext.GetConfiguration as ISSViewConfiguration).GetPrimaryModelName);
  InitToolBar(_GridDBTableView);
end;

procedure TSSDataGridView.InitToolBar(AGrid: TcxGridDBTableView);
var
  Index: Integer;
begin
  if not AGrid.DataController.DataSource.DataSet.Active then
    Exit;

  if AGrid.ColumnCount > 0 then
  begin
    for Index := 0 to AGrid.ColumnCount - 1 do
    begin
      FieldNameListBox.Properties.Items.Add(_GridDBTableView.Columns[Index].DataBinding.Field.DisplayLabel);
    end; //Properties.

  end;
end;

procedure TSSDataGridView.SetCustomeConfiguration(ACustomeConfiguration: ISSConfiguration);
var
  GroupField: TStringList;
  FieldConfiguration: ISSViewExtendFieldConfiguration;
  Index: Integer;
  GroupFieldName: string;
begin
  GroupField := TStringList.Create;
  GroupFieldName := '';

  for Index := 0 to (ACustomeConfiguration as ISSViewExtendConfiguration).GetFieldConfiguration.GetCount - 1 do
  begin
    FieldConfiguration := (ACustomeConfiguration as ISSViewExtendConfiguration).GetFieldConfiguration.GetObject(Index) as ISSViewExtendFieldConfiguration;

    if FieldConfiguration.IsGroup then
      GroupField.Add(FieldConfiguration.GetFieldName + '=' + IntToStr(FieldConfiguration.GetGroupOrder));

    _GridDBTableView.GetColumnByFieldName(FieldConfiguration.GetFieldName).Visible := FieldConfiguration.IsDisplay;
    _GridDBTableView.GetColumnByFieldName(FieldConfiguration.GetFieldName).Index := FieldConfiguration.GetDisplayOrder;
    case FieldConfiguration.GetSortOrder of
      sssoASC: _GridDBTableView.GetColumnByFieldName(FieldConfiguration.GetFieldName).SortOrder := soAscending;
      sssoDESC: _GridDBTableView.GetColumnByFieldName(FieldConfiguration.GetFieldName).SortOrder := soDescending;
    end;

    if FieldConfiguration.GetAggregateType <> ssatNone then
      AppendAggregate(FieldConfiguration.GetFieldName, FieldConfiguration.GetAggregateType, FieldConfiguration.IsAggregateGroup);
  end;

  for Index := 0 to GroupField.Count - 1 do
    _GridDBTableView.GetColumnByFieldName(GroupField.Names[Index]).GroupIndex := StrToInt(GroupField.Values[GroupField.Names[Index]]);

  GroupField.Free;
end;

procedure TSSDataGridView._GridDBTableViewKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  inherited;
  if Key = 17 then
    BeginSelection;
end;

procedure TSSDataGridView._GridDBTableViewKeyUp(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  inherited;
  if Key = 17 then
    EndSelection;
end;

end.
